//
//  NetworkTool.swift
//  Yun
//
//  Created by 新宝 on 2017/9/20.
//  Copyright © 2017年 新宝. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import SnapKit

protocol NetworkToolProtocol {
    //获取体育数据
    static func loadSportsData(controller:String,completionHandler:@escaping(_ sportModel:[SportModel])->())
    //获取头条数据
    static func loadHeadlinesData(controller:String,completionHandler:@escaping(_ headlinesModel:[HeadlinesModel])->())
    //获取财经数据
    static func loadEconomicData(controller:String,completionHandler:@escaping(_ economicModel:[EconomicsModel])->())
    //获取娱乐数据
    static func loadRecreationData(controller:String,completionHandler:@escaping(_ recreationModel:[RecreationModel])->())
    //获取军事数据
    static func loadMilitaryData(controller:String,completionHandler:@escaping(_ militaryModel:[MilitaryModel])->())
    //获取历史数据
    static func loadHistoryData(controller:String,completionHandler:@escaping(_ historyModel:[HistoryModel])->())
    //获取科技数据
    static func loadTechnologyData(controller:String,completionHandler:@escaping(_ technologyModel:[TechnologyModel])->())
    
    //单独一个方法
    static func loadAllData(controllerTitle:String,completionHandler:@escaping(_ arrayModel:[Any])->())
    
}

class NetworkTool:NetworkToolProtocol {
    
    class func loadAllData(controllerTitle: String, completionHandler: @escaping ([Any]) -> ()) {
        switch controllerTitle {
        case "体育":
            let Sports="http://api.3g.ifeng.com/ClientNews?id=TY43,FOCUSTY43&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
            Alamofire.request(Sports).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
                if((response.result.value == nil)){
                    return;
                }
                let json=JSON(response.result.value!)
                let sportModelArr=NSMutableArray()
                for i in 0...1{
                    for item in json[i]["item"]
                    {
                        let  myItem=item.1["thumbnail"].stringValue
                        let myLink=item.1["link"]["weburl"].stringValue
                        let myTitle=item.1["title"].stringValue
                        let myUpdateTime=item.1["updateTime"].stringValue
                        let sportModel=SportModel()
                        sportModel.thumbnail=myItem
                        sportModel.commentsUrl=myLink
                        sportModel.title=myTitle
                        sportModel.updateTime=myUpdateTime
                        sportModelArr.add(sportModel)
                        
                    }
                }
                completionHandler(sportModelArr as! [SportModel])
            })
        case "头条":
            let Headlines="http://api.3g.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Headlines).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let headlineModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let headlinesModel=HeadlinesModel()
                    headlinesModel.thumbnail=myItem
                    headlinesModel.commentsUrl=myLink
                    headlinesModel.title=myTitle
                    headlinesModel.updateTime=myUpdateTime
                    headlineModelArr.add(headlinesModel)
                }
            }
            completionHandler(headlineModelArr as! [HeadlinesModel])
            
        })

            case "财经":
            let Economic="http://api.3g.ifeng.com/ClientNews?id=CJ33,FOCUSCJ33&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Economic).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let economicsModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let economicsModel=EconomicsModel()
                    economicsModel.thumbnail=myItem
                    economicsModel.commentsUrl=myLink
                    economicsModel.title=myTitle
                    economicsModel.updateTime=myUpdateTime
                    economicsModelArr.add(economicsModel)
                }
            }
            completionHandler(economicsModelArr as! [EconomicsModel])
        })
            
            case "娱乐":
                let Recreation="http://api.3g.ifeng.com/ClientNews?id=YL53,FOCUSYL53&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
                Alamofire.request(Recreation).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
                    if((response.result.value == nil)){
                        return;
                    }
                    let json=JSON(response.result.value!)
                    let recreationModelArr=NSMutableArray()
                    for i in 0...1{
                        for item in json[i]["item"]
                        {
                            let  myItem=item.1["thumbnail"].stringValue
                            let myLink=item.1["link"]["weburl"].stringValue
                            let myTitle=item.1["title"].stringValue
                            let myUpdateTime=item.1["updateTime"].stringValue
                            let recreationModel=RecreationModel()
                            recreationModel.thumbnail=myItem
                            recreationModel.commentsUrl=myLink
                            recreationModel.title=myTitle
                            recreationModel.updateTime=myUpdateTime
                            recreationModelArr.add(recreationModel)
                        }
                    }
                    completionHandler(recreationModelArr as! [RecreationModel])
                })

            case "军事":
                let Military="http://api.3g.ifeng.com/ClientNews?id=JS83,FOCUSJS83&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
                Alamofire.request(Military).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
                    if((response.result.value == nil)){
                        return;
                    }
                    let json=JSON(response.result.value!)
                    let  militaryModelArr=NSMutableArray()
                    for i in 0...1{
                        for item in json[i]["item"]
                        {
                            let  myItem=item.1["thumbnail"].stringValue
                            let myLink=item.1["link"]["weburl"].stringValue
                            let myTitle=item.1["title"].stringValue
                            let myUpdateTime=item.1["updateTime"].stringValue
                            let militaryModel=MilitaryModel()
                            militaryModel.thumbnail=myItem
                            militaryModel.commentsUrl=myLink
                            militaryModel.title=myTitle
                            militaryModel.updateTime=myUpdateTime
                            militaryModelArr.add(militaryModel)
                        }
                    }
                    completionHandler(militaryModelArr as! [MilitaryModel])
                })

            case "历史":
                let History="http://api.3g.ifeng.com/ClientNews?id=LS153,FOCUSLS153&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
                Alamofire.request(History).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
                    if((response.result.value == nil)){
                        return;
                    }
                    let json=JSON(response.result.value!)
                    let  historyModelArr=NSMutableArray()
                    for i in 0...1{
                        for item in json[i]["item"]
                        {
                            let  myItem=item.1["thumbnail"].stringValue
                            let myLink=item.1["link"]["weburl"].stringValue
                            let myTitle=item.1["title"].stringValue
                            let myUpdateTime=item.1["updateTime"].stringValue
                            let historyModel=HistoryModel()
                            historyModel.thumbnail=myItem
                            historyModel.commentsUrl=myLink
                            historyModel.title=myTitle
                            historyModel.updateTime=myUpdateTime
                            historyModelArr.add(historyModel)
                        }
                    }
                    completionHandler(historyModelArr as! [HistoryModel])
                })

            case "科技":
                let Technology="http://api.3g.ifeng.com/ClientNews?id=KJ123,FOCUSKJ123&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
                Alamofire.request(Technology).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
                    if((response.result.value == nil)){
                        return;
                    }
                    let json=JSON(response.result.value!)
                    let  technologyModelArr=NSMutableArray()
                    for i in 0...1{
                        for item in json[i]["item"]
                        {
                            let  myItem=item.1["thumbnail"].stringValue
                            let myLink=item.1["link"]["weburl"].stringValue
                            let myTitle=item.1["title"].stringValue
                            let myUpdateTime=item.1["updateTime"].stringValue
                            let technologyModel=TechnologyModel()
                            technologyModel.thumbnail=myItem
                            technologyModel.commentsUrl=myLink
                            technologyModel.title=myTitle
                            technologyModel.updateTime=myUpdateTime
                            technologyModelArr.add(technologyModel)
                        }
                    }
                    completionHandler(technologyModelArr as! [TechnologyModel])
                })
           default:
            break
        }
    }
    
    
    
    
     //体育新闻
    class func loadSportsData(controller: String, completionHandler: @escaping ([SportModel]) -> ()) {
    let Sports="http://api.3g.ifeng.com/ClientNews?id=TY43,FOCUSTY43&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Sports).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
             let sportModelArr=NSMutableArray()
                        for i in 0...1{
                            for item in json[i]["item"]
                            {
                                let  myItem=item.1["thumbnail"].stringValue
                                let myLink=item.1["link"]["weburl"].stringValue
                                let myTitle=item.1["title"].stringValue
                                let myUpdateTime=item.1["updateTime"].stringValue
                                let sportModel=SportModel()
                                sportModel.thumbnail=myItem
                                sportModel.commentsUrl=myLink
                                sportModel.title=myTitle
                                sportModel.updateTime=myUpdateTime
                                sportModelArr.add(sportModel)
                               
                            }
                        }
            completionHandler(sportModelArr as! [SportModel])
        })
        
}
    
    //头条新闻
    class func loadHeadlinesData(controller: String, completionHandler: @escaping ([HeadlinesModel]) -> ()) {
        let Headlines="http://api.3g.ifeng.com/ClientNews?id=SYLB10,SYDT10,SYRECOMMEND&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
         Alamofire.request(Headlines).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let headlineModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let headlinesModel=HeadlinesModel()
                    headlinesModel.thumbnail=myItem
                    headlinesModel.commentsUrl=myLink
                    headlinesModel.title=myTitle
                    headlinesModel.updateTime=myUpdateTime
                    headlineModelArr.add(headlinesModel)
                }
            }
            completionHandler(headlineModelArr as! [HeadlinesModel])
            
         })
}
    
    //财经新闻
    class func loadEconomicData(controller: String, completionHandler: @escaping ([EconomicsModel]) -> ()) {
         let Economic="http://api.3g.ifeng.com/ClientNews?id=CJ33,FOCUSCJ33&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Economic).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let economicsModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let economicsModel=EconomicsModel()
                    economicsModel.thumbnail=myItem
                    economicsModel.commentsUrl=myLink
                    economicsModel.title=myTitle
                    economicsModel.updateTime=myUpdateTime
                    economicsModelArr.add(economicsModel)
                }
            }
            completionHandler(economicsModelArr as! [EconomicsModel])
        })

}
    
    //娱乐新闻
    class func loadRecreationData(controller: String, completionHandler: @escaping ([RecreationModel]) -> ()) {
         let Recreation="http://api.3g.ifeng.com/ClientNews?id=YL53,FOCUSYL53&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Recreation).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let recreationModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let recreationModel=RecreationModel()
                    recreationModel.thumbnail=myItem
                    recreationModel.commentsUrl=myLink
                    recreationModel.title=myTitle
                    recreationModel.updateTime=myUpdateTime
                    recreationModelArr.add(recreationModel)
                }
            }
            completionHandler(recreationModelArr as! [RecreationModel])
        })

}
    
    //军事新闻
    class func loadMilitaryData(controller: String, completionHandler: @escaping ([MilitaryModel]) -> ()) {
        let Military="http://api.3g.ifeng.com/ClientNews?id=JS83,FOCUSJS83&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Military).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let  militaryModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let militaryModel=MilitaryModel()
                    militaryModel.thumbnail=myItem
                    militaryModel.commentsUrl=myLink
                    militaryModel.title=myTitle
                    militaryModel.updateTime=myUpdateTime
                    militaryModelArr.add(militaryModel)
                }
            }
            completionHandler(militaryModelArr as! [MilitaryModel])
        })
}
   
    class func loadHistoryData(controller: String, completionHandler: @escaping ([HistoryModel]) -> ()) {
        let History="http://api.3g.ifeng.com/ClientNews?id=LS153,FOCUSLS153&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(History).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let  historyModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let historyModel=HistoryModel()
                    historyModel.thumbnail=myItem
                    historyModel.commentsUrl=myLink
                    historyModel.title=myTitle
                    historyModel.updateTime=myUpdateTime
                    historyModelArr.add(historyModel)
                }
            }
            completionHandler(historyModelArr as! [HistoryModel])
        })
}
    
    class func loadTechnologyData(controller: String, completionHandler: @escaping ([TechnologyModel]) -> ()) {
         let Technology="http://api.3g.ifeng.com/ClientNews?id=KJ123,FOCUSKJ123&gv=4.5.8&av=0&proid=ifengnews&os=ios_8.3&vt=5&screen=750x1334&publishid=4002&uid=7ae2caf1e42c491f0e711c187d08dc98a0a9e970"
        Alamofire.request(Technology).responseData(queue: DispatchQueue.main, completionHandler: { (response) in
            if((response.result.value == nil)){
                return;
            }
            let json=JSON(response.result.value!)
            let  technologyModelArr=NSMutableArray()
            for i in 0...1{
                for item in json[i]["item"]
                {
                    let  myItem=item.1["thumbnail"].stringValue
                    let myLink=item.1["link"]["weburl"].stringValue
                    let myTitle=item.1["title"].stringValue
                    let myUpdateTime=item.1["updateTime"].stringValue
                    let technologyModel=TechnologyModel()
                    technologyModel.thumbnail=myItem
                    technologyModel.commentsUrl=myLink
                    technologyModel.title=myTitle
                    technologyModel.updateTime=myUpdateTime
                    technologyModelArr.add(technologyModel)
                }
            }
            completionHandler(technologyModelArr as! [TechnologyModel])
        })
}
}
